package ir.values.users.instructions.jump;

import ir.types.Type;
import ir.types.datatypes.VoidType;
import ir.values.BasicBlock;
import ir.values.Value;
import ir.values.users.instructions.InstOperator;
import ir.values.users.instructions.Instruction;

public class ConditionalBrInst extends JumpInst {
    private final Value conditionValue;
    private final BasicBlock jumpTrueBlock;
    private final BasicBlock jumpFalseBlock;
    public ConditionalBrInst(Value conditionValue, BasicBlock trueBasicBlock, BasicBlock falseBasicBlock) {
        super(VoidType.VOID);
        this.conditionValue = conditionValue;
        this.jumpTrueBlock = trueBasicBlock;
        this.jumpFalseBlock = falseBasicBlock;
        operands.add(conditionValue);
        operands.add(trueBasicBlock);
        operands.add(falseBasicBlock);
    }

    @Override
    public String parseLLVM() {
        StringBuilder sb = new StringBuilder();
        sb
                .append(getInstOperator())
                .append(" ")
                .append(conditionValue.getType())
                .append(" ")
                .append(conditionValue.parseUsingName())
                .append(", ")
                .append(jumpTrueBlock.getType())
                .append(" ")
                .append(jumpTrueBlock.parseUsingName())
                .append(", ")
                .append(jumpFalseBlock.getType())
                .append(" ")
                .append(jumpFalseBlock.parseUsingName());
        return sb.toString();
    }

    @Override
    public boolean shouldGenerateId() {
        return false;
    }
    @Override
    public InstOperator getInstOperator() {
        return InstOperator.br;
    }
}
